//
//  CKHttpCommunicate.m
//  MumMum
//
//  Created by shlity on 16/6/16.
//  Copyright © 2016年 Moresing Inc. All rights reserved.
//

#import "CKHttpCommunicate.h"
#import "HttpCommunicateDefine.h"
#import "AFHTTPSessionManager.h"
#import "MBProgressHUD+Add.h"

#define TIME_NETOUT     20.0f

@implementation CKHttpCommunicate
{
    AFHTTPSessionManager *_HTTPManager;
}

- (id)init
{
    if (self = [super init])
    {
        _HTTPManager = [AFHTTPSessionManager manager];
        _HTTPManager.requestSerializer.HTTPShouldHandleCookies = YES;
        
        _HTTPManager.requestSerializer  = [AFHTTPRequestSerializer serializer];
        _HTTPManager.responseSerializer = [AFHTTPResponseSerializer serializer];
        
        
        [_HTTPManager.requestSerializer setTimeoutInterval:TIME_NETOUT];
        
        //把版本号信息传导请求头中
        [_HTTPManager.requestSerializer setValue:[NSString stringWithFormat:@"iOS-%@",[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"]] forHTTPHeaderField:@"MM-Version"];
        
        [_HTTPManager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept" ];
        _HTTPManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json",@"text/html", @"text/plain",nil];
        
        
    }
    return self;
}

+ (id)sharedInstance
{
    static CKHttpCommunicate * HTTPCommunicate;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        HTTPCommunicate = [[CKHttpCommunicate alloc] init];
    });
    return HTTPCommunicate;
}


+ (void)createRequest:(HTTP_COMMAND_LIST)taskID
            WithParam:(NSDictionary *)param
           withMethod:(HTTPRequestMethod)method
              success:(void(^)(id result))success
              failure:(void(^)(NSError *erro))failure
              showHUD:(UIView *)showView
{
    if (taskID < HTTP_METHOD_RESERVE)
    {
        [[CKHttpCommunicate sharedInstance] createUnloginedRequest:taskID WithParam:param withMethod:method success:success failure:failure showHUD:showView];
    }
}

- (void)createUnloginedRequest:(HTTP_COMMAND_LIST)taskID WithParam:(NSDictionary *)param withMethod:(HTTPRequestMethod)method success:(void(^)(id result))success failure:(void(^)(NSError *erro))failure showHUD:(UIView *)showView
{
    
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];

    /**
     *  请求的时候给一个转圈的状态
     */
    if (showView) {
        [MBProgressHUD showProgress:showView];
    }
    
    NSString * url = [NSString stringWithUTF8String:cHttpMethod[taskID]];
    NSString *URLString = [NSString stringWithFormat:@"%@%@",URL_BASE,url];

    /******************************************************************/
    /**
     *  将cookie通过请求头的形式传到服务器，比较是否和服务器一致
     */
    
    NSData *cookiesData = [[NSUserDefaults standardUserDefaults] objectForKey:@"Cookie"];
    
    if([cookiesData length]) {
        /**
         *  拿到所有的cookies
         */
        NSArray *cookies = [NSKeyedUnarchiver unarchiveObjectWithData:cookiesData];
        
        for (NSHTTPCookie *cookie in cookies) {
            /**
             *  判断cookie是否等于服务器约定的ECM_ID
             */
            if ([cookie.name isEqualToString:@"ECM_ID"]) {
                //实现了一个管理cookie的单例对象,每个cookie都是NSHTTPCookie类的实例,将cookies传给服务器
                [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
            }
        }
    }
    
    /******************************************************************/
    
    NSMutableURLRequest *request = [_HTTPManager.requestSerializer requestWithMethod:[self getStringForRequestType:method] URLString:[[NSURL URLWithString:URLString relativeToURL:_HTTPManager.baseURL] absoluteString] parameters:param error:nil];

    NSURLSessionDataTask *dataTask = [_HTTPManager dataTaskWithRequest:request completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
        if (error) {
            
            if (showView) {
                [MBProgressHUD hideHUDForView:showView];
            }
            
            UIWindow *window = [[UIApplication sharedApplication] keyWindow];
            if (error.code == -1009) {
                [MBProgressHUD showError:@"网络已断开" toView:window];
            }else if (error.code == -1005){
                [MBProgressHUD showError:@"网络连接已中断" toView:window];
            }else if(error.code == -1001){
                [MBProgressHUD showError:@"请求超时" toView:window];
            }else if (error.code == -1003){
                [MBProgressHUD showError:@"未能找到使用指定主机名的服务器" toView:window];
            }else{
                [MBProgressHUD showError:[NSString stringWithFormat:@"code:%ld %@",error.code,error.localizedDescription] toView:window];
            }
            
            if (failure != nil)
            {
                failure(error);
            }
            
        } else {
            
            if (success != nil)
            {
                [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
                
                if (showView) {
                    [MBProgressHUD hideHUDForView:showView];
                }
                
                /**
                 *  获取服务器传的请求头cookie信息，并保存
                 */
                
                NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL: [NSURL URLWithString:URL_BASE]];
                
                NSData *data = [NSKeyedArchiver archivedDataWithRootObject:cookies];
                
                [[NSUserDefaults standardUserDefaults] setObject:data forKey:@"Cookie"];
                
                id result = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
                
                success(result);
            }
        }
    }];
    
    [dataTask resume];
}

+ (void)createRequest:(HTTP_COMMAND_LIST)taskID
            WithParam:(NSDictionary*)param
          withExParam:(NSDictionary*)Exparam
           withMethod:(HTTPRequestMethod)method
              success:(void (^)(id result))success
              uploadFileProgress:(void(^)(NSProgress *uploadProgress))uploadFileProgress
              failure:(void (^)(NSError* erro))failure

{
    if (taskID < HTTP_METHOD_RESERVE)
    {
        [[CKHttpCommunicate sharedInstance] createUnloginedRequest:taskID WithParam:param withExParam:Exparam withMethod:method success:success failure:failure uploadFileProgress:uploadFileProgress];
    }
}


- (void)createUnloginedRequest:(HTTP_COMMAND_LIST)taskID WithParam:(NSDictionary *)param withExParam:(NSDictionary*)Exparam withMethod:(HTTPRequestMethod)method success:(void(^)(id result))success failure:(void(^)(NSError *erro))failure uploadFileProgress:(void(^)(NSProgress *uploadProgress))uploadFileProgress
{
    
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
    
    /******************************************************************/
    
    //    NSString * url = [NSString stringWithUTF8String:cHttpMethod[taskID]];
    NSString* url =@"/digiops/upload/picture";
    NSString *URLString = [NSString stringWithFormat:@"%@%@",kBaseUrl,url];
    /*******************/
    //需要提交的数据
    NSData  *data_f = [Exparam objectForKey:@"logo"];
//    NSData  *data_s = UIImageJPEGRepresentation(fImage, 0.8);
//    NSDictionary  * dic = [NSMutableDictionary new];
//    [dic setValue:@"value1" forKey:@"key1"];
//    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil];
    
    //配置AF
//    AFHTTPSessionManager *manage = [AFHTTPSessionManager manager];
//    [manage.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
//    manage.requestSerializer = [AFHTTPRequestSerializer serializer];
//    manage.responseSerializer = [AFHTTPResponseSerializer serializer];
//    manage.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html", @"text/json", @"text/javascript",@"text/plain", nil];
//
////        NSMutableDictionary *newdic = [NSMutableDictionary new];
////        [newdic setObject:data_f forKey:@"logo"];
//    [manage POST:URLString parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
//        //当提交一张图片或一个文件的时候 name 可以随便设置，服务端直接能拿到，如果服务端需要根据name去取不同文件的时候，则appendPartWithFileData 方法中的 name 需要根据form的中的name一一对应
////        [formData appendPartWithFormData:jsonData name:@"UploadData"];
//        [formData appendPartWithFileData: data_f name:@"logo" fileName:@"logo" mimeType:@"image/jpeg"];
////        [formData appendPartWithFileData: data_s name:@"photoS" fileName:@"b.jpg" mimeType:@"image/jpeg"];
//    } progress:^(NSProgress * _Nonnull uploadProgress) {
//
//    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        NSDictionary *responseDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//    }];
    
//    [HttpTool post:url params:newdic success:^(id responseObj) {
//
//
//    } failure:^(NSError *error) {
//
//    }];
    
//    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
//    manager.responseSerializer= [AFHTTPResponseSerializer serializer];
////        NSMutableDictionary *newdic = [NSMutableDictionary new];
////        [newdic setObject:data_f forKey:@"logo"];
//    [manager.requestSerializer setValue:@"multipart/form-data" forHTTPHeaderField:@"Content-Type"];
//    [manager.requestSerializer setValue:[NSString stringWithFormat:@"access_token %@", [MySingleton sharedMySingleton].tokenID] forHTTPHeaderField:@"Authorization"];
//    [manager POST:URLString parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
//        [formData appendPartWithFileData:data_f name:@"logo" fileName:@"logo" mimeType:@"image/png"];
//        NSLog(@"%@", formData);
//    } progress:^(NSProgress * _Nonnull uploadProgress) {
//
//    } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
//
//        NSString *str = [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
//        NSLog(@"%@", str);
//
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        NSLog(@"上传失败%@",error);
//    }];
    /*
    NSMutableURLRequest *mutalbeURLRequest = [[NSMutableURLRequest alloc]init];

    mutalbeURLRequest.HTTPMethod = @"POST";
    mutalbeURLRequest.URL = [NSURL URLWithString:URLString];

    NSString *boundary = [NSString stringWithFormat:@"Boundary+%08X%08X", arc4random(), arc4random()];

    mutalbeURLRequest.allHTTPHeaderFields = @{
                                              @"Content-Type": [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary],
                                              @"Authorization": [NSString stringWithFormat:@"access_token %@", [MySingleton sharedMySingleton].tokenID]
                                              };

#pragma mark - multipart/form-data格式按照构建上传数据

    NSMutableData *postData = [[NSMutableData alloc]init];

     NSDictionary *bodyDict = newdic;

     for (NSString *key in bodyDict) {
     NSString *pair = [NSString stringWithFormat:@"--%@\r\nContent-Disposition: form-data; name=\"%@\"\r\n\r\n",boundary,key];
     [postData appendData:[pair dataUsingEncoding:NSUTF8StringEncoding]];

     id value = [bodyDict objectForKey:key];
     if ([value isKindOfClass:[NSString class]]) {
     [postData appendData:[value dataUsingEncoding:NSUTF8StringEncoding]];
     }else if ([value isKindOfClass:[NSData class]]){
     [postData appendData:value];
     }
     [postData appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
     }
    

    //文件部分

//    NSString *filePair = [NSString stringWithFormat:@"--%@\r\nContent-Disposition: form-data; name = \"%@\"; filename = \"%@\"; Content-Type = %@\r\n\r\n", boundary, @"logo", @"jjj.png", @"image/png"];
//
//    [postData appendData:[filePair dataUsingEncoding:NSUTF8StringEncoding]];
//
//    [postData appendData:data_f]; //加入文件的数据
//
    [postData appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    [mutalbeURLRequest setValue:[NSString stringWithFormat:@"%lu",(unsigned long)postData.length] forHTTPHeaderField:@"Content-Length"];

    [[[NSURLSession sharedSession] uploadTaskWithRequest:mutalbeURLRequest fromData:postData completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSLog(@"--- %@", [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
        NSLog(@"%@", response);
        NSLog(@"==== %@", error);
    }]resume];
    */
//    NSMutableDictionary *newdic = [NSMutableDictionary new];
//    [newdic setObject:data_f forKey:@"logo"];
//    [manage POST:URLString parameters:newdic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        if (success) {
//            [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
//            //            if (responseObject[@"token"]) {
//            //                success(responseObject);
//            //            }
//        }
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        if (failure) {
//            failure(error);
//            //            [UITool showMBProgressHud:[UIApplication sharedApplication].keyWindow withText:@"加载失败，稍后再试" withTime:1.0];
//            [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
//        }
//    }];
    
    /**
     *  将cookie通过请求头的形式传到服务器，比较是否和服务器一致
     */

    NSData *cookiesData = [[NSUserDefaults standardUserDefaults] objectForKey:@"Cookie"];

    if([cookiesData length]) {
        /**
         *  拿到所有的cookies
         */
        NSArray *cookies = [NSKeyedUnarchiver unarchiveObjectWithData:cookiesData];

        for (NSHTTPCookie *cookie in cookies) {
            /**
             *  判断cookie是否等于服务器约定的ECM_ID
             */
            if ([cookie.name isEqualToString:@"ECM_ID"]) {
                //实现了一个管理cookie的单例对象,每个cookie都是NSHTTPCookie类的实例,将cookies传给服务器
                [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
            }
        }
    }

    /******************************************************************/
    NSMutableURLRequest *request = [_HTTPManager.requestSerializer multipartFormRequestWithMethod:[self getStringForRequestType:method] URLString:URLString parameters:param constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        //图片上传
        if (Exparam) {
            for (NSString *key in [Exparam allKeys]) {
//                [formData appendPartWithFileData:[Exparam objectForKey:key] name:key fileName:[NSString stringWithFormat:@"%@.png",key] mimeType:@"image/jpeg"];
                [formData appendPartWithFormData:[Exparam objectForKey:key] name:key];
            }
        }

    } error:nil];

    NSURLSessionDataTask *dataTask = [_HTTPManager dataTaskWithRequest:request uploadProgress:^(NSProgress * _Nonnull uploadProgress) {


        if (uploadProgress) { //上传进度
            uploadFileProgress (uploadProgress);
        }
    } downloadProgress:^(NSProgress * _Nonnull downloadProgress) {

    } completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
        if (error) {

            UIWindow *window = [[UIApplication sharedApplication] keyWindow];

            if (error.code == -1009) {
                [MBProgressHUD showError:@"网络已断开" toView:window];
            }else if (error.code == -1005){
                [MBProgressHUD showError:@"网络连接已中断" toView:window];
            }else if(error.code == -1001){
                [MBProgressHUD showError:@"请求超时" toView:window];
            }else if (error.code == -1003){
                [MBProgressHUD showError:@"未能找到使用指定主机名的服务器" toView:window];
            }else{
                [MBProgressHUD showError:[NSString stringWithFormat:@"code:%ld %@",error.code,error.localizedDescription] toView:window];
            }

            if (failure != nil)
            {
                failure(error);
            }

        } else {

            if (success != nil)
            {
                [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];

                /**
                 *  获取服务器传的请求头cookie信息，并保存
                 */

                NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL: [NSURL URLWithString:URL_BASE]];

                NSData *data = [NSKeyedArchiver archivedDataWithRootObject:cookies];

                [[NSUserDefaults standardUserDefaults] setObject:data forKey:@"Cookie"];

                id result = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];

                success(result);
            }
        }

    }];

    [dataTask resume];
}

+ (void)createDownloadFileWithURLString:(NSString *)URLString
             downloadFileProgress:(void(^)(NSProgress *downloadProgress))downloadFileProgress
                    setupFilePath:(NSURL*(^)(NSURLResponse *response))setupFilePath
        downloadCompletionHandler:(void (^)(NSURL *filePath, NSError *error))downloadCompletionHandler
{
    if (URLString) {
        [[CKHttpCommunicate sharedInstance]createUnloginedDownloadFileWithURLString:URLString downloadFileProgress:downloadFileProgress setupFilePath:setupFilePath downloadCompletionHandler:downloadCompletionHandler];
    }
}

- (void)createUnloginedDownloadFileWithURLString:(NSString *)URLString
                            downloadFileProgress:(void(^)(NSProgress *downloadProgress))downloadFileProgress
                                   setupFilePath:(NSURL*(^)(NSURLResponse *response))setupFilePath
                       downloadCompletionHandler:(void (^)(NSURL *filePath, NSError *error))downloadCompletionHandler
{
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:URLString] cachePolicy:1 timeoutInterval:15];
    
    NSURLSessionDownloadTask *dataTask = [_HTTPManager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
        
        /**
         *  下载进度
         */
        downloadFileProgress(downloadProgress);
        
    } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
        
        /**
         *  设置保存目录
         */
        
        return setupFilePath(response);
        
    } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
        
        /**
         *  下载完成
         */
        downloadCompletionHandler(filePath,error);
    
    }];
    
    [dataTask resume];
}

#pragma mark - GET Request type as string

-(NSString *)getStringForRequestType:(HTTPRequestMethod)type {
    
    NSString *requestTypeString;
    
    switch (type) {
        case POST:
            requestTypeString = @"POST";
            break;
            
        case GET:
            requestTypeString = @"GET";
            break;
            
        case PUT:
            requestTypeString = @"PUT";
            break;
            
        case DELETE:
            requestTypeString = @"DELETE";
            break;
            
        default:
            requestTypeString = @"POST";
            break;
    }

    return requestTypeString;
}

-(NSString*)DataTOjsonString:(id)object
{
    NSString *jsonString = nil;
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:object
                                                       options:NSJSONWritingPrettyPrinted // Pass 0 if you don't care about the readability of the generated string
                                                         error:&error];
    if (! jsonData) {
        NSLog(@"Got an error: %@", error);
    } else {
        jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    }
    return jsonString;
}

@end
